<?php
/**
 * tpAdmin [a web admin based ThinkPHP5]
 *
 * @author yuan1994 <tianpian0805@gmail.com>
 * @link http://tpadmin.yuan1994.com/
 * @copyright 2016 yuan1994 all rights reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */

//------------------------
// 用户模型
//-------------------------

namespace app\common\model;

use think\Model;
use think\Db;
class DispatchTravelerGroup extends Model
{
    public function insert_group($group)
    {
        $input=$group;
        $traveler_ids=$input['traveler_ids'];
        $group_fees = $input['driver_fee'];
        //分组

        $map['id']=array('in',$traveler_ids);
        $order_ids=Db::name("order_traveler")->where($map)->group("oid")->column('oid');

        $dispatch_id=$this->get_dispatch_id($order_ids);
        if(!$dispatch_id){
            $dispatch_id=Db::name("dispatch")->insertGetId(array('dispatch_status'=>'not_dispatch'));
        }

        $_del_dispatch_order_ids=Db::name("dispatch_order")->where(array('dispatch_id'=>$dispatch_id))->column('oid');
        unset($input['id']);
        $input['dispatch_id']=$dispatch_id;

            $group_id=Db::name('dispatch_traveler_group')->insertGetId($input);

        //分配订单
        foreach ($order_ids as $key=>$val){
            $duplicate=Db::name('dispatch_order')->where(array('dispatch_id'=>$dispatch_id,'oid'=>$val))->find();
            if(!$duplicate){
                Db::name("dispatch_order")->insert(array('dispatch_id'=>$dispatch_id,'oid'=>$val));
            }

        }
        $sales_ids=Db::name('order')
            ->where("sales_id is not null and sales_id<> ''")
            ->where('id','in',$order_ids)
            ->column('sales_id');
        if($sales_ids){
            $sales_ids_str=implode(',',$sales_ids);
        }

        $get_an_order=Db::name('order')->where('id','in',$order_ids)->find();
        $car_form=$get_an_order['car_form'];
        Db::name("dispatch_traveler_group")->where('id',$group_id)->update(array('sales_id'=>$sales_ids_str,'car_form'=>$car_form,'group_fees'=>$group_fees));
        $dispatch['is_dispatch_order']=true;


        $travelers=Db::name("order_traveler")->where($map)->select();

        //修改订单旅客状态group_id
        Db::name("order_traveler")->where($map)->update(array('group_id'=>$group_id));


        //分配旅客
        $dispatch_travelers=[];
        foreach ($travelers as $traveler){
            $dispatch_traveler['dispatch_id']=$dispatch_id;
            $dispatch_traveler['oid']=$traveler['oid'];
            $dispatch_traveler['traveler_id']=$traveler['id'];
            $dispatch_traveler['group_id']=$group_id;
            $dispatch_travelers[]=$dispatch_traveler;
        }
        $duplicate_traveler=Db::name("dispatch_traveler")->where('traveler_id','in',$traveler_ids)->find();
        if($duplicate_traveler){
            return false;
        }
        Db::name('dispatch_traveler')->insertAll($dispatch_travelers);

        $dispatch['is_dispatch_traveller']=true;

        $dispatch['is_traveller_group']=true;

        $driver_user_id=$input['driver_user_id'];
        if($driver_user_id){
            $driver=Db::name('driver')->where(array('user_id'=>$driver_user_id))->find();

            $dispatch['is_dispatch_driver']=true;
        }
        Db::name('dispatch')->where(array('id'=>$dispatch_id))->update($dispatch);
        //更改订单状态
        $order_ids=Db::name('dispatch_traveler')->where('group_id',$group_id)->column('oid');
        $order_ids=array_merge($order_ids,$_del_dispatch_order_ids);

        $_map=[];
        $_map['id']=array('in',$order_ids);
        Db::name('order')->where($_map)->update(array('isallot'=>true,'allot_end'=>true));
        //订单里旅客是否已分配完-start
        //获取未分配完旅客的订单id
        $_map_not_allot_traveler['oid']=array('in',$order_ids);
        $not_allot_traveler_order_id=Db::name('order_traveler')
            ->where($_map_not_allot_traveler)
            ->where(function($query){
                $query->where('group_id is null')
                    ->whereOr('group_id',"=","0");
            })
            ->column('oid');
        if($not_allot_traveler_order_id){
            $_map_allot_end_false['id']=array('in',$not_allot_traveler_order_id);
            Db::name('order')->where($_map_allot_end_false)->update(array('allot_end'=>false));
        }

        return $group_id;
    }
    public function edit_group($group)
    {
        $input=$group;
        $traveler_ids=$input['traveler_ids'];
        //分组
        $map['id']=array('in',$traveler_ids);
        $order_ids=Db::name("order_traveler")->where($map)->group("oid")->column('oid');




        $group=$input;

        $group_id=$group['id'];
        unset($group['id']);


        //分配司机

        Db::name('dispatch_traveler_group')->where('id',$group_id)->update($group);
//                echo Db::name('dispatch_traveler_group')->getLastSql();
        $group=Db::name('dispatch_traveler_group')->where('id',$group_id)->find();
        $dispatch_id=$group['dispatch_id'];

        //删除原来订单分配
        //1、要删除的
        $_del_dispatch_order_ids=Db::name("dispatch_order")->where(array('dispatch_id'=>$dispatch_id))->column('oid');
        //2、不可删除的
        $not_del_dispatch_order_ids=Db::name('dispatch_traveler')->where(array('dispatch_id'=>$dispatch_id,'group_id'=>array('neq',$group_id)))->column('oid');
        //获取真正可删除的
        $read_del_dispatch_order_ids=[];
        foreach ($_del_dispatch_order_ids as $del_oid){
            if(!in_array($del_oid,$not_del_dispatch_order_ids)){
                $read_del_dispatch_order_ids[]=$del_oid;
            }
        }
        Db::name('dispatch_order')->where(array('dispatch_id'=>$dispatch_id,'oid'=>array('in',$read_del_dispatch_order_ids)))->delete();
        //分配订单
        foreach ($order_ids as $key=>$val){
            if(!Db::name("dispatch_order")->where(array('dispatch_id'=>$dispatch_id,'oid'=>$val))->find()){
                Db::name("dispatch_order")->insert(array('dispatch_id'=>$dispatch_id,'oid'=>$val));
            }

        }
        $sales_ids=Db::name('order')->where('id','in',$order_ids)->column('sales_id');
        if($sales_ids){
            $sales_ids_str=implode(',',$sales_ids);
        }
        Db::name("dispatch_traveler_group")->where('id',$group_id)->update(array('sales_id'=>$sales_ids_str));
        $dispatch['is_dispatch_order']=true;


        $travelers=Db::name("order_traveler")->where($map)->select();


        //修改原来订单旅客状态group_id
        Db::name("order_traveler")->where(array('group_id'=>$group_id))->update(array('group_id'=>0));

        //修改订单旅客状态group_id
        Db::name("order_traveler")->where($map)->update(array('group_id'=>$group_id));

        //删除原来分配
        Db::name('dispatch_traveler')->where(array('group_id'=>$group_id))->delete();

        //分配旅客
        $dispatch_travelers=[];
        foreach ($travelers as $traveler){
            $dispatch_traveler['dispatch_id']=$dispatch_id;
            $dispatch_traveler['oid']=$traveler['oid'];
            $dispatch_traveler['traveler_id']=$traveler['id'];
            $dispatch_traveler['group_id']=$group_id;
            $dispatch_travelers[]=$dispatch_traveler;
        }

        Db::name('dispatch_traveler')->insertAll($dispatch_travelers);

        $dispatch['is_dispatch_traveller']=true;

        $dispatch['is_traveller_group']=true;

        Db::name('dispatch')->where(array('id'=>$dispatch_id))->update($dispatch);

        //更改订单状态
        $order_ids=Db::name('dispatch_traveler')->where('group_id',$group_id)->column('oid');
        $order_ids=array_merge($order_ids,$_del_dispatch_order_ids);

        $_map=[];
        $_map['id']=array('in',$order_ids);
        Db::name('order')->where($_map)->update(array('isallot'=>true,'allot_end'=>true));
        //订单里旅客是否已分配完-start
        //获取未分配完旅客的订单id
        $_map_not_allot_traveler['oid']=array('in',$order_ids);
        $not_allot_traveler_order_id=Db::name('order_traveler')
            ->where($_map_not_allot_traveler)
            ->where(function($query){
                $query->where('group_id is null')
                    ->whereOr('group_id',"=","0");
            })
            ->column('oid');
        if($not_allot_traveler_order_id){
            $_map_allot_end_false['id']=array('in',$not_allot_traveler_order_id);
            Db::name('order')->where($_map_allot_end_false)->update(array('allot_end'=>false));
        }
        //订单里旅客是否已分配完-end
        return $group_id;
    }
    public function get_dispatch_id($order_id){
        $map['oid']=array('in',$order_id);
        $dispatch_id=Db::name('dispatch_order')->where($map)->value('dispatch_id');
        return $dispatch_id;
    }
}